Professional ASP.NET MVC 3 by Jon Galloway & Phil Haack & Brad Wilson & K. Scott Allen

Professional ASP.NET MVC 3 by Jon Galloway & Phil Haack & Brad Wilson & K. Scott Allen

Author:Jon Galloway & Phil Haack & Brad Wilson & K. Scott Allen
Language: eng
Format: epub
Publisher: John Wiley & Sons
Published: 2011-07-17T16:00:00+00:00


The tmpl method combines the template with the JSON data to produce real DOM elements. Because the JSON data is an array of artists, the template engine renders the template once for each artist in the array. The code takes the template output and appends the output to the artist list.

Client-side templates are a powerful technology, and this section is only scratching the surface of the template engine features. However, the code is not on par with the behavior of the AJAX helper from earlier in the chapter. If you remember from the “AJAX Helpers” section earlier in the chapter, the AJAX helper had the ability to call a method if the server threw an error. The helper also turned on an animated gif while the request was outstanding. You can implement all these features, too; you just have to remove one level of abstraction.

jQuery.ajax for Maximum Flexibility

When you need complete control over an AJAX request, you can turn to the jQuery ajax method. The ajax method takes an options parameter where you can specify the HTTP verb (such as GET or POST), the timeout, an error handler, and more. All the other asynchronous communication methods you've seen (load and getJSON) ultimately call down to the ajax method.

Using the ajax method, you can achieve all the functionality you had with the AJAX helper and still use client-side templates:

$("#artistSearch").submit(function (event) { event.preventDefault(); var form = $(this); $.ajax({ url: form.attr("action"), data: form.serialize(), beforeSend: function () { $("#ajax-loader").show(); }, complete: function () { $("#ajax-loader").hide(); }, error: searchFailed, success: function (data) { $("#artistTemplate").tmpl(data).appendTo("#artist-list"); } }); });



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.